﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZP.Common;
using ZP.DTO.Common;
using ZP.DTO.ht_type1_title;
using ZP.IBLL;
using ZP.Model;
using Z.EntityFramework.Plus;

namespace ZP.BLL
{
    public class ht_type1_titleService : BaseService<ht_type1_title, int>, Iht_type1_titleService
    {
        public PageModel<ht_type1_titleResultDTO> GetHTType1Title(ht_type1_titleSearchDTO dto)
        {
            var psie = (ZPJ_Entities)_repository.GetDbContext();

            PageModel<ht_type1_titleResultDTO> result = new PageModel<ht_type1_titleResultDTO>();
            var iquery = from ft in psie.ht_type1_title.AsNoTracking()
                         join ht in psie.ht_honor_title on ft.ht_id equals ht.ht_id
                         into ht_join
                         from ht in ht_join.DefaultIfEmpty()
                         where ft.is_delete == false && ft.ht_id == dto.htid
                         select new ht_type1_titleResultDTO
                         {
                             ft_id = ft.ft_id,
                             ft_title = ft.ft_title,
                             is_delete = ft.is_delete,
                             ft_sort = ft.ft_sort,
                             ht_id = ft.ht_id,
                             ht_title = ht.ht_title
                         };

            //序列化类型为“System.Data.Entity.DynamicProxies.xxxx”的对象时检测到循环引用。 加上这个就OK了
            psie.Configuration.ProxyCreationEnabled = false;

            if (null != dto)
            {
                if (!string.IsNullOrWhiteSpace(dto.SearchText))
                {
                    iquery = iquery.Where(p => p.ft_title.Contains(dto.SearchText));
                }
            }
            result.count = iquery.Count();
            iquery = iquery.OrderBy(m => m.ft_id).Skip((dto.page - 1) * dto.limit).Take(dto.limit);
            //result.rows = Mapper.Map<List<fz_sys_user>, List<fz_sys_userResultDTO>>(iquery.ToList());
            //result.data = AutoMapper.Mapper.Map<List<ht_type1_titleResultDTO>>(iquery.ToList());
            result.data = iquery.ToList();
            return result;
        }

        public bool SaveHTType1Title(ht_type1_titleOperateDTO dto)
        {
            bool result = false;
            using (var psie = (ZPJ_Entities)_repository.GetDbContext())
            {
                if (0 == dto.ft_id || !dto.ft_id.HasValue)
                {
                    //判断是否已经存在用户账号和员工
                    bool existsCount = psie.ht_type1_title.AsNoTracking().Any(p => p.is_delete == false && p.ft_title == dto.ft_title && p.ht_id == dto.ht_id);
                    if (existsCount)
                    {
                        throw new Exception(TipsMessage.该附件标题已存在);
                    }

                    //新增
                    var item = Mapper.Map<ht_type1_titleOperateDTO, ht_type1_title>(dto);
                    item.is_delete = false;
                    _repository.Add(item, false);
                    // 记录日志
                    //this.AddLog(psie, new LogData
                    //{
                    //    CurrentLogType = LogType.ADD,
                    //    LogDetails = "新增用户" + dto.account,
                    //    OpratorName = dto.updater
                    //});
                }
                else
                {
                    ht_type1_title item = _repository.Find(dto.ft_id.Value);

                    //判断是否已经存在用户账号和员工
                    bool existsCount = psie.ht_type1_title.AsNoTracking().Any(p => p.is_delete == false && p.ft_title == dto.ft_title && p.ft_id != dto.ft_id && p.ht_id == dto.ht_id);
                    if (existsCount)
                    {
                        throw new Exception(TipsMessage.该附件标题已存在);
                    }
                    item.ft_title = dto.ft_title;
                    item.ft_sort = dto.ft_sort;
                    //item = Mapper.Map<ht_honor_title>(dto);
                    //修改
                    //item.state = ZPFlagType.YES;
                    _repository.Update(item, false);

                    // 记录日志
                    //this.AddLog(psie, new LogData
                    //{
                    //    CurrentLogType = LogType.MODIFY,
                    //    LogDetails = "修改用户" + dto.account,
                    //    OpratorName = dto.updater
                    //});
                }
                result = _repository.SaveChanges();
            }
            return result;
        }

        public bool DeleteHTType1Title(List<ht_type1_titleOperateDTO> dto, UserLoginDTO loginUser)
        {
            bool result = false;
            if (dto == null || dto.Count <= 0)
            {
                return result;
            }
            List<int> longIds = dto.Select(i => i.ft_id.Value).ToList();
            //查询角色下是否还存在用户，存在则不能删除
            var entitys = _repository.GetModels(p => longIds.Contains(p.ft_id));

            //删除用户
            //Delete(psie, users);
            int updateCount = entitys.Update(p => new ht_type1_title() { is_delete = true });
            if (updateCount > 0)
            {
                string str_ht_title = string.Join(",", entitys.Select(o => o.ft_title).ToList());
                // 记录日志
                //this.AddLog(psie, new LogData
                //{
                //    CurrentLogType = LogType.DELETE,
                //    LogDetails = "删除用户" + strUserName,
                //    OpratorName = loginUser.account
                //});
            }
            result = updateCount > 0;
            return result;
        }

    }
}
